package com.example.base.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.base.config.PageData;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.system.domain.SysApi;
import com.example.base.system.domain.vo.SysApiVo;
import com.example.base.system.service.SysApiService;
import com.example.base.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.base.limit.IPRateLimit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;

import java.io.Serializable;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author X.I.O
 * @since 2024-04-10
 */

@RestController
@Api(tags = {"的服务类"})
@RequestMapping("/sys-api")
public class SysApiController  {

        @Autowired
        private SysApiService sysApiService;


        @PostMapping("/saveorupdate")
        @IPRateLimit(name = "SysApi-saveorupdate",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的新增/更新接口",notes = "的新增接口")
        public JSONObject save(@RequestBody @Validated SysApiVo sysApiVo) throws GlobalExcetion{
                sysApiService.saveOrUpdateSysApi(sysApiVo);
                return Result.successInfo("操作成功");
        }


        @PostMapping("/save")
        @IPRateLimit(name = "SysApi-save",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的新增接口",notes = "的新增接口")
        public JSONObject save(@RequestBody SysApi sysApi) throws GlobalExcetion {
                 sysApiService.saveSysApi(sysApi);
                 return Result.successInfo("保存成功");
        }


        @PostMapping("/update")
        @IPRateLimit(name = "SysApi-update",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的更新接口",notes = "的更新接口")
        public JSONObject update(@RequestBody SysApi sysApi) throws GlobalExcetion{
                sysApiService.updateSysApiById(sysApi);
                return Result.successInfo("更新成功");
        }



        @DeleteMapping("/delbyid")
        @IPRateLimit(name = "SysApi-del",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的删除接口",notes = "的删除接口")
        public JSONObject delete(@RequestParam("id") Serializable id) throws GlobalExcetion{
                boolean flag = sysApiService.removeById(id);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @PostMapping("/del/batch")
        @IPRateLimit(name = "SysApi-delbatch",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的批量删除接口",notes = "的批量删除接口")
        public JSONObject deleteBatch(@RequestBody List<Integer> ids) throws GlobalExcetion {
                boolean flag =  sysApiService.removeByIds(ids);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @GetMapping("/getall")
        @IPRateLimit(name = "SysApi-getall",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的获取全部接口",notes = "的获取全部接口")
        public JSONObject findAll() throws GlobalExcetion {
                List<SysApi> list= sysApiService.list();
                return Result.successInfo(list);
        }

        @GetMapping("/getbyid")
        @IPRateLimit(name = "SysApi-getid",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的获取一个接口",notes = "的获取一个接口")
        public JSONObject findOne(@RequestParam("id") Serializable id) throws GlobalExcetion {
                SysApi data= sysApiService.getById(id);
                return Result.successInfo(data);
        }

        @GetMapping("/page")
        @IPRateLimit(name = "SysApi-page",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的分页接口",notes = "的分页接口")
        public JSONObject findPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) throws GlobalExcetion {
                IPage<SysApi> data=sysApiService.page(page, pageSize);
                PageData pageData=new PageData(data.getTotal(),page,pageSize,data.getRecords());
                return Result.successInfo(pageData);
        }
}

